package world2d;
import data.GlobalData;
import data.Object2DData;
import openfl.display.Bitmap;
import openfl.display.Sprite;
import box2D.collision.shapes.B2PolygonShape;
import box2D.dynamics.B2Body;
import box2D.dynamics.B2BodyDef;
import box2D.dynamics.B2DebugDraw;
import box2D.dynamics.B2FixtureDef;
import box2D.dynamics.B2World;
import box2D.collision.B2Collision;
/**
 * ...
 * @author ...
 */
class Avatar extends Sprite
{
	private var m_cWorld:B2World;
	public var m_cBody:B2Body;
	private var sd:B2PolygonShape;
	private var bd:B2BodyDef;
	private	var b:B2Body;
	private var fd:B2FixtureDef ;

	public var m_nHits:Int = 0;
	private var m_cCannon:Cannon;
	public var m_cBullet:Bullet2D;
	
	public function new(cWorld:B2World) 
	{
		super();
		// Add bodies
		fd = new B2FixtureDef();
		sd = new B2PolygonShape();
		bd = new B2BodyDef();
		bd.type = B2Body.b2_dynamicBody;
		//bd.isBullet = true;
		
		fd.density = 1.0;
		fd.friction = 0.5;
		fd.restitution = 0.1;
		fd.shape = sd;
		m_cWorld = cWorld;
	}
	
	public function setImage(bm:Bitmap)
	{
	}
	
	public function createBox(nWidth:Int, nHeight:Int, nPosX:Int, nPosY:Int):Void
	{
	
		
		sd.setAsBox((nWidth) / GlobalData.PHYSIC_SCALE, (nHeight) / GlobalData.PHYSIC_SCALE);
		bd.position.set((nPosX) / GlobalData.PHYSIC_SCALE, (nPosY) / GlobalData.PHYSIC_SCALE);
		b = m_cWorld.createBody(bd);
		b.createFixture(fd);
		var cData :Object2DData = new Object2DData();
		cData.m_nName = "avatar";
		b.setUserData(cData);
		

	
		
		m_cBullet = new Bullet2D(m_cWorld);
		m_cBullet.create();
		this.addChild(m_cBullet);
		
		var cCannon:Cannon = new Cannon(m_cBullet);
		this.addChild(cCannon);
		
		cCannon.y = nPosY;
		cCannon.x = nPosX ;
		
		if (GlobalData.m_nWidth / 2 > nPosX)
		{
		
			cCannon.setDirection(1);
		}
		else
		{
		
			
			cCannon.setDirection(-1);
			}
	}
	
	public 	function hittedBy(cObj2dData:Object2DData )
	{
		m_nHits++;
		trace("hits : " + m_nHits);
		onHitted(m_nHits);
	}
	
	dynamic public function onHitted(ncnt:Int){}
}